﻿<%@ WebHandler Language="C#" Class="communityOperate" %>

using System;
using System.Web;
using System.IO;
using System.Text;

public class communityOperate : AjaxHandlerBase{

    override public void RequestProcess(HttpContext context)
    {
        string type = "";
        string strwhere = "";
        StringBuilder output = new StringBuilder();
        
        //获得类型
        if (!string.IsNullOrEmpty(context.Request.QueryString["type"]))
        {
            type = context.Request.QueryString["type"];
        }
        
        //执行楼盘名称验证
        if (type == "ncheck")
        {
            if (!string.IsNullOrEmpty(context.Request.QueryString["txtCommunity"]))
            {
                strwhere = context.Request.QueryString["txtCommunity"];
            }
           bool flag = !Chenxin.Lib.House.DAL.Community_BaseInfo.Exists("CommunityName", strwhere);
           output.Append("{\"success\":"+flag.ToString()+"}"); 
        }

        //执行楼盘地址验证
        else if (type == "acheck")
        {
            if (!string.IsNullOrEmpty(context.Request.QueryString["txtaddress"]))
            {
                strwhere = context.Request.QueryString["txtaddress"];
            }
            bool flag = !Chenxin.Lib.House.DAL.Community_BaseInfo.Exists("Address", strwhere);
            output.Append("{\"success\":" + flag.ToString() + "}");
        }
            
        //执行楼盘拼音验证
        else if (type == "pycheck")
        {
            if (!string.IsNullOrEmpty(context.Request.QueryString["txtPY"]))
            {
                strwhere = context.Request.QueryString["txtPY"];
            }
            bool flag = !Chenxin.Lib.House.DAL.Community_BaseInfo.Exists("PY", strwhere);
            output.Append("{\"success\":" + flag.ToString() + "}");
        }

        //执行楼栋名称验证
        else if (type == "bcheck")
        {
            string txtBuildingName = "";
            string houseid = "";
            if (!string.IsNullOrEmpty(context.Request.QueryString["txtBuildingName"]))
            {
                txtBuildingName = context.Request.QueryString["txtBuildingName"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["houseid"]))
            {
                houseid = context.Request.QueryString["houseid"];
            }
            strwhere = string.Format("BuildingName='{0}' and CommunityId='{1}'", txtBuildingName, houseid);
            bool flag = !Chenxin.Lib.House.DAL.Community_BuildingInfo.Exists(strwhere);
            output.Append("{\"success\":" + flag.ToString() + "}");
        }

        //执行添加楼盘               
        else if (type == "addcom")
        {
            Chenxin.Lib.House.Entity.Community_BaseInfo comm = new Chenxin.Lib.House.Entity.Community_BaseInfo();
            if (!string.IsNullOrEmpty(context.Request.QueryString["CommunityName"]))
            {
                comm.CommunityName = context.Request.QueryString["CommunityName"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Region"]))
            {
                comm.Region = context.Request.QueryString["Region"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Block"]))
            {
                comm.Block = context.Request.QueryString["Block"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Address"]))
            {
                comm.Address = context.Request.QueryString["Address"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Address2"]))
            {
                comm.Address2 = context.Request.QueryString["Address2"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Address3"]))
            {
                comm.Address3 = context.Request.QueryString["Address3"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Address4"]))
            {
                comm.Address4 = context.Request.QueryString["Address4"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["CommunityUse"]))
            {
                comm.CommunityUse = context.Request.QueryString["CommunityUse"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["BuildingType"]))
            {
                comm.BuildingType = context.Request.QueryString["BuildingType"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["CompleteYear"]))
            {
                comm.CompleteYear = int.Parse(context.Request.QueryString["CompleteYear"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Schools"]))
            {
                comm.Schools = context.Request.QueryString["Schools"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Parks"]))
            {
                comm.Parks = context.Request.QueryString["Parks"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Traffics"]))
            {
                comm.Traffics = context.Request.QueryString["Traffics"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Description"]))
            {
                comm.Description = context.Request.QueryString["Description"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["PY"]))
            {
                comm.PY = context.Request.QueryString["PY"];
            }
            comm.AddTime = DateTime.Now;
            comm.AddPerson = CurrentUserInfo.UserName;
            comm.CommunityId = System.Guid.NewGuid();
            Chenxin.Lib.House.DAL.Community_BaseInfo.Add(comm);
            output.Append("{success:'1',communityid:'" + comm.CommunityId + "'}");
        }

        else if (type == "addbuild")
        {
            Chenxin.Lib.House.Entity.Community_BuildingInfo build = new Chenxin.Lib.House.Entity.Community_BuildingInfo();
            if (!string.IsNullOrEmpty(context.Request.QueryString["BuildingName"]))
            {
                build.BuildingName = context.Request.QueryString["BuildingName"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Floor"]))
            {
                build.Floor = int.Parse(context.Request.QueryString["Floor"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["UnitNum"]))
            {
                build.UnitNum = int.Parse(context.Request.QueryString["UnitNum"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["UnitType"]))
            {
                build.UnitType = int.Parse(context.Request.QueryString["UnitType"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["RoomRule"]))
            {
                build.RoomRule = context.Request.QueryString["RoomRule"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["RoomRuleRemark"]))
            {
                build.RoomRuleRemark = context.Request.QueryString["RoomRuleRemark"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["CommunityId"]))
            {
                build.CommunityId = new Guid(context.Request.QueryString["CommunityId"]);
            }
            build.AddPerson = CurrentUserInfo.UserName;
            build.AddTime = DateTime.Now;
            build.BuildingId = Guid.NewGuid();
            Chenxin.Lib.House.DAL.Community_BuildingInfo.Add(build);
            output.Append("{\"success\":'1',\"BuildingId\":'" + build.BuildingId + "'}");
        }

        else if (type == "getbuild")
        {
            string buildingid = "";
            if (!string.IsNullOrEmpty(context.Request.QueryString["BuildingId"]))
            {
                buildingid = context.Request.QueryString["BuildingId"];
            }
            Chenxin.Lib.House.Entity.Community_BuildingInfo build = Chenxin.Lib.House.DAL.Community_BuildingInfo.GetModel(new Guid(buildingid));

            if (build != null)
            {
                output.Append("{success: '1',BuildingId:'" + build.BuildingId + "',BuildingName:'"
                    + build.BuildingName + "',Floor:'" + build.Floor + "',UnitNum:'"
                    + build.UnitNum + "',UnitType:'" + build.UnitType + "',RoomRule:'"
                    + build.RoomRule + "',AddTime:'" + build.AddTime + "',AddPerson:'" + build.AddPerson + "',CommunityId:'" + build.CommunityId + "'}");
            }
            else
                output.Append("{success:'0'}");
        }

        else if (type == "upbuild")
        {
            Chenxin.Lib.House.Entity.Community_BuildingInfo build = new Chenxin.Lib.House.Entity.Community_BuildingInfo();
            if (!string.IsNullOrEmpty(context.Request.QueryString["BuildingName"]))
            {
                build.BuildingName = context.Request.QueryString["BuildingName"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["Floor"]))
            {
                build.Floor = int.Parse(context.Request.QueryString["Floor"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["UnitNum"]))
            {
                build.UnitNum = int.Parse(context.Request.QueryString["UnitNum"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["UnitType"]))
            {
                build.UnitType = int.Parse(context.Request.QueryString["UnitType"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["RoomRule"]))
            {
                build.RoomRule = context.Request.QueryString["RoomRule"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["RoomRuleRemark"]))
            {
                build.RoomRuleRemark = context.Request.QueryString["RoomRuleRemark"];
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["CommunityId"]))
            {
                build.CommunityId = new Guid(context.Request.QueryString["CommunityId"]);
            }
            if (!string.IsNullOrEmpty(context.Request.QueryString["BuildingId"]))
            {
                build.BuildingId = new Guid(context.Request.QueryString["BuildingId"]);
            }
            build.AddPerson = CurrentUserInfo.UserName;
            build.AddTime = DateTime.Now;

            Chenxin.Lib.House.DAL.Community_BuildingInfo.Update(build);
            output.Append("{\"success\":'1',\"BuildingId\":'" + build.BuildingId + "'}");
        }

        else if (type == "delbuild")
        {
            string id = "";
            if (!string.IsNullOrEmpty(context.Request.QueryString["id"]))
            {
                id = context.Request.QueryString["id"];
            }
            Chenxin.Lib.House.DAL.Community_BuildingInfo.Delete(new Guid(id));
            output.Append("{success:'1'}");
        }

        context.Response.ContentType = "text/plain";
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.Write(output);
    }
    override public bool IsReusable
    {
        get {
            return false;
        }
    }
}